//
//  CardRechargeVC.m
//  Gas
//
//  Created by 张伟良 on 17/5/26.
//  Copyright © 2017年 HongHu. All rights reserved.
//

#import "CardRechargeVC.h"
#import "ICUsageDetailVC.h"
#import "ImmediatelyPayVC.h"
#import "RechargeRecordVC.h"
#import "LogView.h"
#import "CardModel.h"
#import "SPBluetoothManager.h"
#import "UILabel+WLAttributedString.h"

@interface CardRechargeVC ()

@property(nonatomic,strong) UILabel *lab5;
@property(nonatomic,strong) UILabel *lab3;
@property(nonatomic,strong) UILabel *lab6;
@property(nonatomic,strong) UIButton *btn2;
@property(nonatomic,strong) UIView *baseView;
@property(nonatomic,strong) UIView *view1;
@property(nonatomic,strong) UITextField *tf;
@property(nonatomic,strong) UIButton *btn3;
@property(nonatomic,strong) UILabel *payLab;
@property(nonatomic,strong) UIButton *btn4;
@property(nonatomic,strong) UIView *line3;
@property(nonatomic,strong) UIScrollView *scrView;
@property(nonatomic,strong) LogView *logView;


@property(nonatomic,strong) NSString *money;
@property(nonatomic,strong) CardModel *cardModel;
@property(nonatomic,strong) NSMutableString *mStr;
@property(nonatomic,strong) SPBluetoothManager *manager;


@end

@implementation CardRechargeVC

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.title = @"XX燃气IC卡自助充值";
    
    SPBluetoothManager *manager = [SPBluetoothManager shareManager];
    self.manager = manager;
    
    [self initRightItem];
    
    [self addCopyDataBlue];
    [self getManyDataByCommunicateNo];
    
    // 缴费成功后写卡
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(paySuccessAction) name:@"kPaySuccessNotification" object:nil];
    
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
//    [self getNotWriteRecord];
}

- (void)logAction:(UIButton *)btn
{
    if ([btn.currentTitle isEqualToString:@"打开日记"]) {
        [btn setTitle:@"关闭日记" forState:UIControlStateNormal];
        self.logView.hidden = NO;
    }
    else {
        [btn setTitle:@"打开日记" forState:UIControlStateNormal];
        self.logView.hidden = YES;
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)initRightItem
{
    UIButton *btn2 = [UIButton buttonWithType:UIButtonTypeCustom];
    btn2.frame = CGRectMake(0, 0, 70, 30);
    [btn2 addTarget:self action:@selector(recordAction) forControlEvents:UIControlEventTouchUpInside];
    [btn2 setTitle:@"充值记录" forState:UIControlStateNormal];
    btn2.titleLabel.font = [UIFont systemFontOfSize:15];
    [btn2 setTitleColor:[UIColor colorWithHexString:@"#0D60FA"] forState:UIControlStateNormal];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:btn2];
}

- (void)recordAction
{
    RechargeRecordVC *vc= [[RechargeRecordVC alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)initSubviews
{
    UIScrollView *scrView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
    [self.view addSubview:scrView];
    self.scrView = scrView;
    
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreen_Width, 0)];
    view.backgroundColor = [UIColor whiteColor];
    [scrView addSubview:view];
    self.baseView = view;
    
    UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(20, 30, 30, 30)];
    imgView.image = [UIImage imageNamed:@"0-1"];
    imgView.contentMode = UIViewContentModeScaleAspectFit;
    [view addSubview:imgView];
    
    UILabel *lab = [[UILabel alloc] initWithFrame:CGRectMake(imgView.right+10, 30, 100, 30)];
    lab.text = @"燃气费";
    lab.textColor = [UIColor colorWithHexString:@"#A1A2A4"];
    [view addSubview:lab];
    
    
    UIButton *setBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    setBtn.frame = CGRectMake(kScreen_Width-50, lab.center.y-10, 20, 20);
    [setBtn setImage:[UIImage imageNamed:@"1-1"] forState:UIControlStateNormal];
    [view addSubview:setBtn];
    
    UIView *line1 = [[UIView alloc] initWithFrame:CGRectMake(0, imgView.bottom+30, kScreen_Width, 1)];
    line1.backgroundColor = [UIColor colorWithHexString:@"#A1A2A4"];
    [view addSubview:line1];
    
    UILabel *lab1 = [[UILabel alloc] initWithFrame:CGRectMake(12, line1.bottom+20, kScreen_Width-24, 16)];
    lab1.text = [NSString stringWithFormat:@"用户编号 : %@",self.cardModel.customerNo];
    lab1.font = [UIFont systemFontOfSize:15];
    lab1.textColor = [UIColor colorWithHexString:@"#A1A2A4"];
    [view addSubview:lab1];
    
    UILabel *lab2 = [[UILabel alloc] initWithFrame:CGRectMake(12, lab1.bottom+20, kScreen_Width-24, 16)];
    lab2.text = [NSString stringWithFormat:@"用户名称 : %@",self.cardModel.customerName];
    lab2.font = [UIFont systemFontOfSize:15];
    lab2.textColor = [UIColor colorWithHexString:@"#A1A2A4"];
    [view addSubview:lab2];
    
    UILabel *lab3 = [[UILabel alloc] initWithFrame:CGRectMake(12, lab2.bottom+20, kScreen_Width-24, 16)];
    lab3.text = [NSString stringWithFormat:@"用户地址 : %@",self.cardModel.address];
    lab3.font = [UIFont systemFontOfSize:15];
    lab3.textColor = [UIColor colorWithHexString:@"#A1A2A4"];
    [view addSubview:lab3];
    self.lab3 = lab3;
    
    
    UILabel *lab5 = [[UILabel alloc] initWithFrame:CGRectMake(12, lab3.bottom+20, kScreen_Width-24, 16)];
    lab5.font = [UIFont systemFontOfSize:15];
    lab5.textColor = [UIColor colorWithHexString:@"#A1A2A4"];
//    lab5.text = [NSString stringWithFormat:@"卡内金额:"];
    [view addSubview:lab5];
    self.lab5 = lab5;
    
    // 燃气费显示
//    self.manager.SurplusMoney = @"0000000020000";
    NSString *str1 = [self.manager.SurplusMoney substringWithRange:NSMakeRange(0, self.manager.SurplusMoney.length-2)];
    NSString *str2 = [self.manager.SurplusMoney substringWithRange:NSMakeRange(self.manager.SurplusMoney.length-2, 2)];
    NSString *money = [NSString stringWithFormat:@"%ld.%@",str1.integerValue, str2];
    self.lab5.text = [NSString stringWithFormat:@"卡内金额:%@",money];
    [self.lab5 wl_changeColorWithTextColor:[UIColor colorWithHexString:@"#EF9B39"] changeText:money];

//    NSMutableAttributedString *attStr = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"卡内金额:%@ 卡内气量:%@ 充值次数:%@",str1, str2, str3]];
    
    
    UILabel *payCompany = [[UILabel alloc] initWithFrame:CGRectMake(12, lab5.bottom+20, kScreen_Width-24, 16)];
    payCompany.text = @"缴费单位 : XX天然气有限公司";
    payCompany.font = [UIFont systemFontOfSize:15];
    payCompany.textColor = [UIColor colorWithHexString:@"#A1A2A4"];
    [view addSubview:payCompany];
    
    
//    UIButton *btn2 = [UIButton buttonWithType:UIButtonTypeCustom];
//    btn2.frame = CGRectMake(50, payCompany.bottom+30, kScreen_Width-100, 40);
//    btn2.layer.cornerRadius = 5;
//    btn2.layer.masksToBounds = YES;
//    [btn2 addTarget:self action:@selector(readerCard) forControlEvents:UIControlEventTouchUpInside];
//    [btn2 setTitle:@"读卡" forState:UIControlStateNormal];
//    btn2.backgroundColor = [UIColor colorWithHexString:@"#55B810"];
//    [view addSubview:btn2];
//    self.btn2 = btn2;
    
    UIView *line3 = [[UIView alloc] initWithFrame:CGRectMake(0, payCompany.bottom+30, kScreen_Width, 1)];
    line3.backgroundColor = [UIColor colorWithHexString:@"#A1A2A4"];
    [view addSubview:line3];
    self.line3 = line3;
    
    [self initRechargeViews];
    
    ////////////
//    UILabel *lab6 = [[UILabel alloc] initWithFrame:CGRectMake(12, view1.bottom+10, kScreen_Width-24, 70)];
    UILabel *lab6 = [[UILabel alloc] initWithFrame:CGRectMake(12, self.view1.bottom+10, kScreen_Width-24, 70)];
    lab6.text = @"提示 ：充值期间请勿将IC卡拔出读卡器，请保持读卡器在手机3米范围内。如遇到充值问题或需退款，请至营业厅办理。";
    lab6.numberOfLines = 3;
    lab6.font = [UIFont systemFontOfSize:14];
    lab6.textColor = [UIColor colorWithHexString:@"#A1A2A4"];
    [view addSubview:lab6];
    self.lab6 = lab6;
    

//    view1.height = btn3.bottom;
    view.height = lab6.bottom+10;
    
    // 调试
    UIButton *btntest = [UIButton buttonWithType:UIButtonTypeCustom];
    btntest.frame = CGRectMake(20, 0, kScreen_Width, 30);
    [btntest addTarget:self action:@selector(logAction:) forControlEvents:UIControlEventTouchUpInside];
    [btntest setTitle:@"打开日记" forState:UIControlStateNormal];
    btntest.titleLabel.font = [UIFont systemFontOfSize:15];
    [btntest setTitleColor:[UIColor colorWithHexString:@"#0D60FA"] forState:UIControlStateNormal];
    [self.view addSubview:btntest];
    
    
    self.logView = [[LogView alloc] initWithFrame:CGRectMake(0, 100, kScreen_Width, 400)];
    [self.view addSubview:self.logView];
    self.logView.hidden = YES;
}

// 获取未写卡的充值记录
- (void)getNotWriteRecord
{
    NSMutableDictionary  *paramDic=[[NSMutableDictionary  alloc]initWithCapacity:0];
    
    if ([SPBluetoothManager shareManager].deviceType1 == 0) {
        [paramDic setValue:[SPBluetoothManager shareManager].cardNo forKey:@"communicateNo"];
        
    }
    else {
        [paramDic setValue:[SPBluetoothManager shareManager].cardNo forKey:@"meterNo"];
        
    }
    
    [AFNetworking_RequestData requestMethodPOSTUrl:GetNotWriteRecord dic:paramDic showHUD:YES response:NO Succed:^(id responseObject) {
        
        
        NSArray *arr = responseObject[@"ListData"];
        
        
    } failure:^(NSError *error) {
        
    }];
}

// 上传抄表信息
- (void)addCopyDataBlue
{
    
    NSMutableDictionary  *paramDic=[[NSMutableDictionary  alloc]initWithCapacity:0];
    [paramDic setValue:self.manager.CardAttTimes forKey:@"CardAttTimes"];
    [paramDic setValue:self.manager.Cumulant forKey:@"Cumulant"];
    [paramDic setValue:self.manager.CurrMonthTotal forKey:@"CurrMonthTotal"];
    [paramDic setValue:self.manager.Last1MonthTotal forKey:@"Last1MonthTotal"];
    [paramDic setValue:self.manager.MagAttTimes forKey:@"MagAttTimes"];
    [paramDic setValue:self.manager.MeterState forKey:@"MeterState"];
    [paramDic setValue:self.manager.OverFlowTimes forKey:@"OverFlowTimes"];
    [paramDic setValue:self.manager.OverZeroMoney forKey:@"OverZeroMoney"];
    [paramDic setValue:self.manager.VersionNumber forKey:@"VersionNumber"];
    [paramDic setValue:self.manager.SurplusMoney forKey:@"SurplusMoney"];
    [paramDic setValue:self.manager.accPrice forKey:@"accPrice"];
    [paramDic setValue:self.manager.alarmMoney forKey:@"alarmMoney"];
    [paramDic setValue:self.manager.cardRegionNo forKey:@"cardAreaId"];
    [paramDic setValue:self.manager.cardNo forKey:@"cardId"];
    
    [paramDic setValue:self.manager.fluidType forKey:@"deviceType"];
    [paramDic setValue:self.manager.endAmount1 forKey:@"endAmount1"];
    [paramDic setValue:self.manager.endAmount2 forKey:@"endAmount2"];
    [paramDic setValue:self.manager.endAmount3 forKey:@"endAmount3"];
    [paramDic setValue:self.manager.fixedPrice forKey:@"fixedPrice"];
    [paramDic setValue:self.manager.fluidType forKey:@"fluidType"];

    [paramDic setValue:self.manager.intervalMonth forKey:@"intervalMonth"];
    [paramDic setValue:self.manager.maxOverMoney forKey:@"maxOverMoney"];
    [paramDic setValue:self.manager.maxOverdraft forKey:@"maxOverdraft"];
    [paramDic setValue:self.manager.price1 forKey:@"price1"];
    [paramDic setValue:self.manager.price2 forKey:@"price2"];

    [paramDic setValue:self.manager.price3 forKey:@"price3"];
    [paramDic setValue:self.manager.startTime forKey:@"startTime"];
    [paramDic setValue:self.manager.sumRechargeMoney forKey:@"sumRechargeMoney"];
    [paramDic setValue:self.manager.sumUseMoney forKey:@"sumUseMoney"];
    [paramDic setValue:self.manager.timeArea1 forKey:@"timeArea1"];
    
    [paramDic setValue:self.manager.timeArea1 forKey:@"timeArea1"];
    [paramDic setValue:self.manager.timeArea2 forKey:@"timeArea2"];
    [paramDic setValue:self.manager.timeArea3 forKey:@"timeArea3"];
    [paramDic setValue:self.manager.timePrice1 forKey:@"timePrice1"];
    [paramDic setValue:self.manager.timePrice2 forKey:@"timePrice2"];
    [paramDic setValue:self.manager.timePrice3 forKey:@"timePrice3"];
    [paramDic setValue:self.manager.weekUseVolume forKey:@"weekUseVolume"];


    [AFNetworking_RequestData requestMethodPOSTUrl:AddCopyDataBlue dic:paramDic showHUD:YES response:NO Succed:^(id responseObject) {
        
        
    } failure:^(NSError *error) {
        
    }];
}

// 获取设备相关信息
- (void)getManyDataByCommunicateNo
{
    
    NSMutableDictionary  *paramDic=[[NSMutableDictionary  alloc]initWithCapacity:0];
    
    if (self.manager.deviceType1 == 0) {
        [paramDic setValue:self.cardNo forKey:@"communicateNo"];
//        [paramDic setValue:@"0000000000" forKey:@"communicateNo"];

    }
    else {
        [paramDic setValue:self.cardNo forKey:@"meterNo"];

    }
    
    [AFNetworking_RequestData requestMethodPOSTUrl:GetManyDataByCommunicateNo dic:paramDic showHUD:YES response:NO Succed:^(id responseObject) {
        

        NSArray *arr = responseObject[@"ListData"];
        
        NSDictionary *dic = [arr firstObject];
        self.cardModel = [CardModel yy_modelWithJSON:dic];
        
        [self initSubviews];

        if (self.cardModel) {
            
            NSMutableString *mStr = [NSMutableString string];
            self.mStr = mStr;
            
            NSMutableString *testStr = [NSMutableString string];

            
            // 价格类型标志
            NSString *aStr3 = [self.cardModel.fluidType substringFromIndex:2];
            NSString *str3 = aStr3;
            [mStr appendString:str3];
            NSLog(@"价格类型标志：%@",str3);
            [testStr appendFormat:@"价格类型标志:%@\n",self.cardModel.fluidType];

            
            // 卡区域号
            NSString *str4 = self.cardRegionNo;
            [mStr appendString:str4];
            NSLog(@"卡区域号：%@",str4);

            
            // 卡ID号
            NSString *str5 = self.cardNo;
            [mStr appendString:str5];
            NSLog(@"卡ID号：%@",str5);

            
            // 周期开始时间
            NSString *timeStr = [self.cardModel.startTime stringByReplacingOccurrencesOfString:@"T" withString:@""];
            timeStr = [timeStr stringByReplacingOccurrencesOfString:@"-" withString:@""];
            timeStr = [timeStr stringByReplacingOccurrencesOfString:@":" withString:@""];
            timeStr = [timeStr substringWithRange:NSMakeRange(2, timeStr.length-4)];
            
            NSString *str6 = timeStr;
            [mStr appendString:str6];
            NSLog(@"周期开始时间：%@",str6);
            [testStr appendFormat:@"周期开始时间:%@\n",self.cardModel.startTime];

            
            // 周期间隔时间
            NSString *str7 = [self generateData:self.cardModel.intervalMonth lenght:2];
            [mStr appendString:str7];
            NSLog(@"周期间隔时间：%@",str7);
            [testStr appendFormat:@"周期间隔时间:%@\n",self.cardModel.intervalMonth];

            
            // 价格版本号
            NSString *str8 = nil;
            
            if (self.cardModel.VersionNumber.integerValue > self.manager.VersionNumber.integerValue) {
                str8 = self.cardModel.VersionNumber;

            }
            else {
                str8 = self.manager.VersionNumber;

            }
            [mStr appendString:str8];
            NSLog(@"价格版本号：%@",str8);
            [testStr appendFormat:@"价格版本号:%@\n",self.cardModel.VersionNumber];

            
            // 阶梯量1
            NSString *str9 = [self generateData:self.cardModel.endAmount1 lenght:2*6];
            [mStr appendString:str9];
            NSLog(@"阶梯量1：%@",str9);
            [testStr appendFormat:@"阶梯量1:%@\n",self.cardModel.endAmount1];

            
            // 单价1
            NSString *str10 = [self generateData:self.cardModel.price1 lenght:2*4];
            [mStr appendString:str10];
            NSLog(@"单价1：%@",str10);
            [testStr appendFormat:@"单价1:%@\n",self.cardModel.price1];

            
            // 阶梯量2
            NSString *str11 = [self generateData:self.cardModel.endAmount2 lenght:2*6];
            [mStr appendString:str11];
            NSLog(@"阶梯量2：%@",str11);
            [testStr appendFormat:@"阶梯量2:%@\n",self.cardModel.endAmount2];

            
            // 单价2
            NSString *str12 = [self generateData:self.cardModel.price2 lenght:2*4];
            [mStr appendString:str12];
            NSLog(@"单价2：%@",str12);
            [testStr appendFormat:@"单价2:%@\n",self.cardModel.price2];

            
            // 阶梯量3
            NSString *str13 = [self generateData:self.cardModel.endAmount3 lenght:2*6];
            [mStr appendString:str13];
            NSLog(@"阶梯量3：%@",str13);
            [testStr appendFormat:@"阶梯量3:%@\n",self.cardModel.endAmount3];

            
            // 单价3
            NSString *str14 = [self generateData:self.cardModel.price3 lenght:2*4];
            [mStr appendString:str14];
            NSLog(@"单价3：%@",str14);
            [testStr appendFormat:@"单价3:%@\n",self.cardModel.price3];

            
            // 分时时间1
            NSString *str15 = [self generateData:self.cardModel.timeArea1 lenght:2*1];
            [mStr appendString:str15];
            NSLog(@"分时时间1：%@",str15);
            [testStr appendFormat:@"分时时间1:%@\n",self.cardModel.timeArea1];

            
            // 分时价格1
            NSString *str16 = [self generateData:self.cardModel.timePrice1 lenght:2*4];
            [mStr appendString:str16];
            NSLog(@"分时价格1：%@",str16);
            [testStr appendFormat:@"分时价格1:%@\n",self.cardModel.timePrice1];

            
            // 分时时间2
            NSString *str17 = [self generateData:self.cardModel.timeArea2 lenght:2*1];
            [mStr appendString:str17];
            NSLog(@"分时时间2：%@",str17);
            [testStr appendFormat:@"分时时间2:%@\n",self.cardModel.timeArea2];

            
            // 分时价格2
            NSString *str18 = [self generateData:self.cardModel.timePrice2 lenght:2*4];
            [mStr appendString:str18];
            NSLog(@"分时价格2：%@",str18);
            [testStr appendFormat:@"分时价格2:%@\n",self.cardModel.timePrice2];

            
            // 分时时间3
            NSString *str19 = [self generateData:self.cardModel.timeArea3 lenght:2*1];
            [mStr appendString:str19];
            NSLog(@"分时时间3：%@",str19);
            [testStr appendFormat:@"分时时间3:%@\n",self.cardModel.timeArea3];

            
            // 分时价格3
            NSString *str20 = [self generateData:self.cardModel.timePrice3 lenght:2*4];
            [mStr appendString:str20];
            NSLog(@"分时价格3：%@",str20);
            [testStr appendFormat:@"分时价格3:%@\n",self.cardModel.timePrice3];

            
            // 定时价格
            NSString *str21 = [self generateData:self.cardModel.fixedPrice lenght:2*4];
            [mStr appendString:str21];
            NSLog(@"定时价格：%@",str21);
            [testStr appendFormat:@"定时价格:%@\n",self.cardModel.fixedPrice];

            
            // 报警金额
            NSString *str22 = [self generateData:self.cardModel.alarmMoney lenght:2*4];
            [mStr appendString:str22];
            NSLog(@"报警金额：%@",str22);
            [testStr appendFormat:@"报警金额:%@\n",self.cardModel.alarmMoney];

            
            // 可透支金额
            NSString *str23 = [self generateData:self.cardModel.maxOverMoney lenght:2*4];
            [mStr appendString:str23];
            NSLog(@"可透支金额：%@",str23);
            [testStr appendFormat:@"可透支金额:%@\n",self.cardModel.maxOverMoney];

            
            // 过流量
            NSString *str24 = [self generateData:self.cardModel.maxOverdraft lenght:2*1];
            [mStr appendString:str24];
            NSLog(@"过流量：%@",str24);
            [testStr appendFormat:@"过流量:%@\n",self.cardModel.maxOverdraft];

            
            // 定时价格启用标志
            NSString *str25 = [self generateData:self.cardModel.PriceFlag lenght:2*1];
            [mStr appendString:str25];
            NSLog(@"定时价格启用标志：%@",str25);
            [testStr appendFormat:@"定时价格启用标志:%@\n",self.cardModel.PriceFlag];

            
            // 时间
            //获取系统当前时间
            NSDate *currentDate = [NSDate date];
            //用于格式化NSDate对象
            NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
            //设置格式：zzz表示时区
            [dateFormatter setDateFormat:@"yyyyMMddHHmm"];
            //NSDate转NSString
            NSString *currentDateString = [dateFormatter stringFromDate:currentDate];
            currentDateString = [currentDateString substringWithRange:NSMakeRange(2, currentDateString.length-2)];
            
            NSString *str26 = currentDateString;
            [mStr appendString:str26];
            NSLog(@"时间：%@",str26);

            
            self.logView.tv1.text = [NSString stringWithFormat:@"===服务器数据===\n%@",testStr];
            self.logView.tv2.text = [NSString stringWithFormat:@"===蓝牙数据===\n%@",self.manager.mStr];

        }


        
    } failure:^(NSError *error) {
        
    }];
}


//// 读卡
//- (void)readerCard
//{
//
//    [self.view endEditing:YES];
//
//    self.view1.hidden = NO;
//    self.view1.height = 100;
//    self.payLab.hidden = YES;
//    self.btn4.hidden = YES;
//    self.lab6.top = self.view1.bottom+10;
//    self.baseView.height = self.lab6.bottom+10;
//
//    self.scrView.contentSize = CGSizeMake(kScreen_Width, self.baseView.height+64);
//
//}


- (void)initRechargeViews
{
    /////////////
    UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(0, self.line3.bottom-1, kScreen_Width, 100)];
    //    view1.backgroundColor = [UIColor whiteColor];
    view1.layer.borderWidth = 1;
    view1.layer.borderColor = [UIColor colorWithHexString:@"#A1A2A4"].CGColor;
//    view1.clipsToBounds = YES;
//    view1.hidden = YES;
    [self.baseView addSubview:view1];
    self.view1 = view1;
    
    // 灰条
    UIView *grayView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreen_Width, 20)];
//    grayView.backgroundColor = [UIColor colorWithHexString:@"#F2F4F8"];
    grayView.backgroundColor = [UIColor whiteColor];
    [view1 addSubview:grayView];
    
    UIButton *checkBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    checkBtn.frame = CGRectMake((kScreen_Width-120)/2, 10, 120, 20);
    [checkBtn addTarget:self action:@selector(detailAction) forControlEvents:UIControlEventTouchUpInside];
    checkBtn.layer.cornerRadius = 5;
    checkBtn.layer.masksToBounds = YES;
    checkBtn.titleLabel.font = [UIFont systemFontOfSize:13];
    [checkBtn setTitle:@"查看价格明细" forState:UIControlStateNormal];
    checkBtn.backgroundColor = [UIColor colorWithHexString:@"#EF9B39"];
    //    btn3.titleLabel.textAlignment = NSTextAlignmentLeft;
    [view1 addSubview:checkBtn];
    
    UILabel *numLab = [[UILabel alloc] initWithFrame:CGRectMake(12, grayView.bottom+30, 230, 16)];
    numLab.font = [UIFont systemFontOfSize:15];
    numLab.textColor = [UIColor colorWithHexString:@"#A1A2A4"];
    numLab.text = [NSString stringWithFormat:@"充值 :                        "];
    [view1 addSubview:numLab];
    
    UITextField *tf = [[UITextField alloc] initWithFrame:CGRectMake(90, grayView.bottom+20, 80, 40)];
    //    view1.backgroundColor = [UIColor whiteColor];
    tf.layer.borderWidth = 1;
    tf.layer.borderColor = [UIColor colorWithHexString:@"#A1A2A4"].CGColor;
    tf.layer.cornerRadius = 5;
    tf.layer.masksToBounds = YES;
    tf.textAlignment = NSTextAlignmentCenter;
    [tf addTarget:self action:@selector(valueChangeAction:) forControlEvents:UIControlEventEditingChanged];
    tf.keyboardType = UIKeyboardTypeNumberPad;
    [view1 addSubview:tf];
    self.tf = tf;
    
    UIButton *btn3 = [UIButton buttonWithType:UIButtonTypeCustom];
    btn3.frame = CGRectMake(kScreen_Width-130-12, numLab.center.y-15, 130, 30);
    [btn3 addTarget:self action:@selector(calculateAction:) forControlEvents:UIControlEventTouchUpInside];
    //    [btn3 setTitleColor:[UIColor colorWithHexString:@"#0D60FA"] forState:UIControlStateNormal];
    btn3.layer.cornerRadius = 5;
    btn3.layer.masksToBounds = YES;
    btn3.titleLabel.font = [UIFont systemFontOfSize:15];
//    [btn3 setTitle:@"计算金额" forState:UIControlStateNormal];
//    btn3.backgroundColor = [UIColor colorWithHexString:@"#E97827"];
    [btn3 setTitle:@"立即充值" forState:UIControlStateNormal];
    btn3.backgroundColor = [UIColor colorWithHexString:@"#154C9A"];
    //    btn3.titleLabel.textAlignment = NSTextAlignmentLeft;
    [view1 addSubview:btn3];
    self.btn3 = btn3;
    
//    UILabel *payLab = [[UILabel alloc] initWithFrame:CGRectMake(12, tf.bottom+20, 200, 16)];
//    payLab.font = [UIFont systemFontOfSize:15];
//    payLab.textColor = [UIColor colorWithHexString:@"#A1A2A4"];
//    payLab.text = [NSString stringWithFormat:@"应缴金额 :                 元"];
//    [view1 addSubview:payLab];
//    self.payLab = payLab;
//
//    UIButton *btn4 = [UIButton buttonWithType:UIButtonTypeCustom];
//    btn4.frame = CGRectMake(kScreen_Width-130-12, payLab.center.y-15, 130, 30);
//    [btn4 addTarget:self action:@selector(rechargeAction) forControlEvents:UIControlEventTouchUpInside];
//    //    [btn3 setTitleColor:[UIColor colorWithHexString:@"#0D60FA"] forState:UIControlStateNormal];
//    btn4.layer.cornerRadius = 5;
//    btn4.layer.masksToBounds = YES;
//    btn4.titleLabel.font = [UIFont systemFontOfSize:15];
//    [btn4 setTitle:@"立即充值" forState:UIControlStateNormal];
//    btn4.backgroundColor = [UIColor colorWithHexString:@"#154C9A"];
//    //    btn3.titleLabel.textAlignment = NSTextAlignmentLeft;
//    [view1 addSubview:btn4];
//    self.btn4 = btn4;
}

- (void)valueChangeAction:(UITextField *)tf
{
    if (tf.text.length > 6) {
        tf.text = [tf.text substringWithRange:NSMakeRange(0, 6)];
    }
}

- (void)detailAction
{
    ICUsageDetailVC *vc = [[ICUsageDetailVC alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
}

//// 监听文本框变化
//- (void)valueChangeAction:(UITextField *)tf
//{
//    self.view1.height = 100;
//    self.payLab.hidden = YES;
//    self.btn4.hidden = YES;
//    self.lab6.top = self.view1.bottom+10;
//    self.baseView.height = self.lab6.bottom+10;
//    self.scrView.contentSize = CGSizeMake(kScreen_Width, self.baseView.height+64);
//
//}



//// 立即充值
//- (void)rechargeAction
//{
//    PaymentModel *model = [[PaymentModel alloc] init];
//    model.payableAmount = self.money;
//
//    ImmediatelyPayVC *vc = [[ImmediatelyPayVC alloc] init];
//    vc.model = model;
//    vc.mark = 1;
//    [self.navigationController pushViewController:vc animated:YES];
//}

// 立即充值
- (void)calculateAction:(UIButton *)btn
{
    [self.view endEditing:YES];
    self.manager.isRecharge = YES;


    if (self.tf.text.length ==0) {
//        [self.view makeToast:@"请输入数量值"];
        [self.view makeToast:@"请输入金额"];
        return;
    }

    
    PaymentModel *model = [[PaymentModel alloc] init];
    model.payableAmount = self.tf.text;

    ImmediatelyPayVC *vc = [[ImmediatelyPayVC alloc] init];
    vc.model = model;
    vc.mark = 1;
    [self.navigationController pushViewController:vc animated:YES];

}

- (void)paySuccessAction
{
    if (self.cardModel) {
        
        if (self.manager.connected) {
            
            self.manager.isRechargeConfirm = NO;
            
            NSMutableString *dataStr = [NSMutableString string];
            [dataStr appendString:self.mStr];
            
            // 充值金额
            NSString *aStr2 = [NSString stringWithFormat:@"%@00",self.tf.text];
            NSString *str2 = [self generateData:aStr2 lenght:2*4];
            [dataStr insertString:str2 atIndex:0];
            NSLog(@"充值金额：%@\n",str2);
            
            // 命令类型
            NSString *str1 = @"20";
            [dataStr insertString:str1 atIndex:0];
            NSLog(@"命令类型：%@\n",str1);
            NSLog(@"数据域:%@，长度%ld",dataStr, dataStr.length);
            
            [self.manager writeCard:dataStr];
        }
        else {
            
            NSString *msg = @"蓝牙已断开,请回到首页重新连接蓝牙";
            
            UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提示" message:msg preferredStyle:UIAlertControllerStyleAlert];
            
            //修改title
            NSMutableAttributedString *alertControllerStr = [[NSMutableAttributedString alloc] initWithString:@"提示"];
            [alertControllerStr addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0, 2)];
            [alertControllerStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:20] range:NSMakeRange(0, 2)];
            [alertController setValue:alertControllerStr forKey:@"attributedTitle"];
            
            
            //修改message
            NSMutableAttributedString *alertControllerMessageStr = [[NSMutableAttributedString alloc] initWithString:msg];
            [alertControllerMessageStr addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0, msg.length)];
            [alertControllerMessageStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:17] range:NSMakeRange(0, msg.length)];
            [alertController setValue:alertControllerMessageStr forKey:@"attributedMessage"];
            
            UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                
                [self.navigationController popViewControllerAnimated:YES];
                
            }];
            [alertController addAction:okAction];
            [self presentViewController:alertController animated:YES completion:nil];
        }
        
    }
}

- (NSString *)generateData:(NSString *)str lenght:(NSInteger)length
{
    if ([str containsString:@"."]) {
        str = [str stringByReplacingOccurrencesOfString:@"." withString:@""];
    }
    
    NSMutableString *mStr = [NSMutableString string];
    NSInteger zeroNum = length - str.length;
    for (int i=0; i<zeroNum; i++) {
        [mStr appendString:@"0"];
    }
    NSString *finalStr = [NSString stringWithFormat:@"%@%@",mStr, str];
    return finalStr;
}

@end
